/*
 * TepcoMeter:TepcoMetterWidgetService.java
 * Copyright © 2011 ho9ho9 All Rights Reserved.
 */
package com.ho9ho9.tepcometer;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import com.ho9ho9.tepcometer.dto.WattDto;
import com.ho9ho9.tepcometer.logic.TepcoMetterLogic;

/**
 * TepcoMetterWidgetServiceクラス.
 * @author ho9ho9
 */
public class TepcoMetterWidgetService extends Service {

	private static final String TAG_NAME = "TepcoMetterWidgetService";
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.d(TAG_NAME, "startId:" + startId);
		// 既存処理呼び出し
		super.onStart(intent, startId);
		
		// Widget情報取得
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
		
		// 当日の消費電力を取得
		TepcoMetterLogic logic = new TepcoMetterLogic();
		WattDto wattDto = logic.getToDayWattInfo(this);
		if (wattDto != null) {
			// 取得結果を反映
			logic.reflectWidgetViews(getResources(), views, wattDto);
			// Widgetを更新
			manager.updateAppWidget(new ComponentName(this, TepcoMetterWidgetProvider.class), views);
		} else {
			// 当日の消費電力を取得できなかった場合
			Log.e(TAG_NAME, "use watt data failed!");
		}
		
		Log.d(TAG_NAME, "service end");
	}
}
